/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package percussiongenerator.gui.view;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import org.jfonia.constants.SymbolConstants;
import org.jfonia.model.Sequence;
import org.jfonia.model.PerformanceNode;
import org.jfonia.model.PerformanceSequence;
import org.jfonia.model.TempoNode;
import org.jfonia.model.elements.TimeSignature;
import org.jfonia.model.elements.Tone;
import percussiongenerator.model.Track;

/**
 *
 * @author jannes
 */
public class MetronomeViewOptions extends ViewOptionsPanel{    
    private JTextField txtBpm,txtNominator,txtDenominator;
    private Track track;
    //Equal<Long> rel1,rel2;
    public MetronomeViewOptions(final Track track) {
        PerformanceSequence seq = track.getStaff().getToneSequence().getPerformanceSequence();
        this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
        this.track = track;
        Box box = Box.createHorizontalBox();
        this.add(box);
        JLabel lbl = new JLabel("BPM:");
        box.add(lbl);
        txtBpm = new JTextField("");
        txtBpm.setMaximumSize(new Dimension(30,20));
        box.add(txtBpm);
        box.add(Box.createHorizontalGlue());
        txtBpm.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try{
                    int bpm = Integer.parseInt(txtBpm.getText());
                    ((TempoNode)track.getStaff().getTempoMap().getIntervalFromIndex(0)).setBPM(bpm);
                    track.getStaff().getToneSequence().setLockPerformance(false);
                }catch(Exception exc){}
            }
        });        
        box = Box.createHorizontalBox();
        this.add(box);
        JLabel lblTimeSignature = new JLabel("Time signature");
        box.add(lblTimeSignature);
        Box vbox = Box.createVerticalBox();
        box.add(vbox);
        txtNominator = new JTextField(5);
        txtNominator.setMaximumSize(new Dimension(20,20));
        txtNominator.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try{
                    int nominator = Integer.parseInt(txtNominator.getText());
                    Sequence<TimeSignature> ts = track.getStaff().getSequence(TimeSignature.class);
                    ((TimeSignature)ts.getIntervalFromIndex(0)).getNominatorNode().setValue(nominator);
                    PerformanceSequence seq = track.getStaff().getToneSequence().getPerformanceSequence();
                    while(seq.size() < nominator){
                        PerformanceNode perNode = ((PerformanceNode)seq.getIntervalFromIndex(0)).clone();
                        perNode.setVelocity(80);
                        seq.add(perNode);
                    }                    
                    while(seq.size() > nominator){
                        seq.remove(seq.size()-1);
                    }
                    PerformanceNode perNode = (PerformanceNode)seq.getIntervalFromIndex(0);
                    perNode.setVelocity(120);
                }catch(Exception exc){
                    exc.printStackTrace();
                }
            }
        });
        txtDenominator = new JTextField(5);
        txtDenominator.setMaximumSize(new Dimension(20,20));
         txtDenominator.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try{
                    int denominator = Integer.parseInt(txtDenominator.getText());
                    Sequence<TimeSignature> ts = track.getStaff().getSequence(TimeSignature.class);
                    if(SymbolConstants.DIVISOR % denominator == 0){
                        ((TimeSignature)ts.getIntervalFromIndex(0)).getDenominatorNode().setValue(denominator);
                        Sequence<Tone> seq = track.getStaff().getToneSequence();
                        int ticks = SymbolConstants.DIVISOR/denominator;
                        for(int i=0; i < seq.size(); i++){
                            ((Tone)(seq.getIntervalFromIndex(i))).getDifferenceNode().setValue(ticks);
                        }
                        track.getStaff().getToneSequence().setLockPerformance(false);
                    }else{
                        txtDenominator.setText("" + ((TimeSignature)ts.getIntervalFromIndex(0)).getDenominator());
                    }

                }catch(Exception exc){
                    exc.printStackTrace();
                }
            }
         });
        vbox.add(txtNominator);
        vbox.add(txtDenominator);
        box.add(Box.createHorizontalGlue());
        this.add(Box.createVerticalGlue());
        setText();
    }

    private void setText(){
        int bpm = ((TempoNode)track.getStaff().getTempoMap().getIntervalFromIndex(0)).getBPM();
        txtBpm.setText("" + bpm);
        TimeSignature ts = (TimeSignature)track.getStaff().getSequence(TimeSignature.class).getIntervalFromIndex(0);
        txtNominator.setText("" + ts.getNominator());
        txtDenominator.setText("" + ts.getDenominator());
    }

    public void removeRelations(){
        /*
        if(rel1 != null){
            rel1.switchOff();
            rel1 = null;
        }
        if(rel2 != null){
            rel2.switchOff();
            rel2 = null;
        }
         */
    }
}
